package pzl.com.bluetooth;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;

import pzl.com.bluetooth.app.CustomApplication;
import pzl.com.bluetooth.bean.BluetoothMessage;
import pzl.com.bluetooth.constant.ConstantUtils;
import pzl.com.bluetooth.receive.CommonReceiver;

/**
 * @author zl.peng
 * @version [1.0, 2016-10-17]
 */
public class BaseActivity extends AppCompatActivity {
    private CommonReceiver commonReceiver;

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    }


    protected void registerBroadcastReceiver(String[] actions) {
        if (actions == null || actions.length == 0) return;

        commonReceiver = new CommonReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (ConstantUtils.ACTION_RECEIVER_BLUETOOTHMESSAGE.equals(intent.getAction())) {
                    onReceiveBluetoothMessage((BluetoothMessage) intent.getSerializableExtra("BluetoothMessage"));
                }
                if (ConstantUtils.ACTION_RECEIVER_CONNECTEDTOSERVER.equals(intent.getAction())) {
                    onConnectedToServer();
                }
                if (ConstantUtils.ACTION_RECEIVER_SERVERACCEPT.equals(intent.getAction())) {
                    onAcceptedNew(intent.getStringExtra("clientAddress"));
                }
            }
        };

        for (String action : actions) {
            registerReceiver(commonReceiver, new IntentFilter(action));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (commonReceiver != null) unregisterReceiver(commonReceiver);
    }


    protected void onReceiveBluetoothMessage(BluetoothMessage bluetoothMessage) {

    }

    /**
     * 客户端连接成功到服务端，client的回调
     */
    protected void onConnectedToServer() {

    }

    /**
     * 服务端接收一个新的连接，service的回调
     *
     * @param clientAddress
     */
    protected void onAcceptedNew(String clientAddress) {

    }

}
